#include "app_inneltest.h"
#include "../imgui/imgui.h"
#include "bgfx/bgfx.h"
#include <fstream>

#include <unistd.h>
#include "bx/math.h"
#include "bimg/bimg.h"
#include "bimg/decode.h"
#include "imgui_impl_bfgx.h"
#include "exportapi.h"
//bgfx::TextureHandle g_frame_tex;
//bgfx::FrameBufferHandle g_framebuffer;
//bgfx::TextureHandle g_ctex;
//bgfx::VertexLayout g_layout;
//bgfx::ProgramHandle g_program;
//
//bgfx::VertexBufferHandle g_vb;
//bgfx::IndexBufferHandle g_ib;
//float g_time;
//struct myvec
//{
//	float x;
//	float y;
//	float z;
//	uint32_t m_argb;
//};
//bgfx::ShaderHandle LoadShader(const char* shadername)
//{
//	char buf[256];
//	_getcwd(buf, 256);
//	std::string search1 = "../";
//	std::string search2 = "../../";
//	std::ifstream file = std::ifstream(shadername, std::ios_base::binary);
//	if (!file)
//	{
//		std::string sfile = search1 + shadername;
//		file = std::ifstream(sfile.c_str(), std::ios_base::binary);
//	}
//	if (!file)
//	{
//		std::string sfile = search2 + shadername;
//		file = std::ifstream(sfile.c_str(), std::ios_base::binary);
//	}
//	if (!file)
//	{
//		return BGFX_INVALID_HANDLE;
//	}
//	file.seekg(0, std::ios::end);
//	auto filelen = (uint32_t)file.tellg();
//	file.seekg(0, std::ios_base::beg);
//
//
//	auto mem = bgfx::alloc(filelen);
//	file.read((char*)mem->data, filelen);
//	return bgfx::createShader(mem);
//}

//
//static myvec s_cubeVertices[] =
//{
//	{-1.0f,  1.0f,  1.0f, 0x01010101 },
//	{ 1.0f,  1.0f,  1.0f, 0xff0101ff },
//	{-1.0f, -1.0f,  1.0f, 0x0101ff01 },
//	{ 1.0f, -1.0f,  1.0f, 0xff01ffff },
//	{-1.0f,  1.0f, -1.0f, 0x01ff0101 },
//	{ 1.0f,  1.0f, -1.0f, 0xffff01ff },
//	{-1.0f, -1.0f, -1.0f, 0xffffff01 },
//	{ 1.0f, -1.0f, -1.0f, 0xffffffff },
//};
//
//static const uint16_t s_cubeTriList[] =
//{
//	0, 1, 2, // 0
//	1, 3, 2,
//	4, 6, 5, // 2
//	5, 6, 7,
//	0, 2, 4, // 4
//	4, 2, 6,
//	1, 5, 3, // 6
//	5, 7, 3,
//	0, 4, 1, // 8
//	4, 5, 1,
//	2, 3, 6, // 10
//	6, 3, 7,
//};

void app_inneltest::onInit()
{
	////Scene_Create(u"hello scene");
	////create rt
	//g_frame_tex = bgfx::createTexture2D(1024, 1024, false, 1, bgfx::TextureFormat::RGBA8, BGFX_TEXTURE_RT);
	//auto texd16 = bgfx::createTexture2D(1024, 1024, false, 1, bgfx::TextureFormat::D16, BGFX_TEXTURE_RT_WRITE_ONLY);
	//bgfx::TextureHandle fbtextures[] = { g_frame_tex,texd16 };
	//g_framebuffer = bgfx::createFrameBuffer(2, fbtextures, true);

	////����vertexbuffer �ڴ沼��
	//g_layout
	//	.begin()
	//	.add(bgfx::Attrib::Enum::Position, 3, bgfx::AttribType::Enum::Float, false)
	//	.add(bgfx::Attrib::Enum::Color0, 4, bgfx::AttribType::Enum::Uint8, true)
	//	.end();

	//std::string shadername = "resources/shaders/";
	//bgfx::RendererType::Enum rtype = bgfx::getRendererType();
	//switch (rtype)
	//{
	//case bgfx::RendererType::Noop:
	//	break;
	//case bgfx::RendererType::Direct3D9:
	//	shadername += "dx9/";
	//	break;
	//case bgfx::RendererType::Direct3D11:
	//	shadername += "dx11/";
	//	break;
	//case bgfx::RendererType::Direct3D12:
	//	break;
	//case bgfx::RendererType::Gnm:
	//	break;
	//case bgfx::RendererType::Metal:
	//	break;
	//case bgfx::RendererType::Nvn:
	//	break;
	//case bgfx::RendererType::OpenGLES:
	//	break;
	//case bgfx::RendererType::OpenGL:
	//	break;
	//case bgfx::RendererType::Vulkan:
	//	break;
	//case bgfx::RendererType::WebGPU:
	//	break;
	//case bgfx::RendererType::Count:
	//	break;
	//default:
	//	break;
	//}

	//auto vs = LoadShader((shadername + "vs_cubes.bin").c_str());
	//auto fs = LoadShader((shadername + "fs_cubes.bin").c_str());
	//g_program = bgfx::createProgram(vs, fs, true);
	//// Create staticvertex buffer.
	//g_vb = bgfx::createVertexBuffer(
	//	bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices)), g_layout
	//);
	//g_ib = bgfx::createIndexBuffer(
	//	bgfx::makeRef(s_cubeTriList, sizeof(s_cubeTriList)));
	//auto dvb = bgfx::createDynamicVertexBuffer(bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices)), g_layout);
	//bgfx::update(dvb, 0, bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices)));

	//�ڲ����Թ����Ѿ�Ǩ��Ϊ������c#����
	//g_ctex = LoadTexture("resources/textures/man01.png");
}

void app_inneltest::onUpdate()
{
	//Frame_Flush();
	////IM_ASSERT(1 != 1 && "what");
	//bgfx::setViewFrameBuffer(0, g_framebuffer);
	//bgfx::setViewFrameBuffer(1, BGFX_INVALID_HANDLE);

	//bgfx::setViewRect(0, 0, 0, 1024, 1024);
	//bgfx::setViewClear(0, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0xFF00FFFF);
	//bgfx::touch(0);


	//const bx::Vec3 at = { 0.0f, 0.0f,   0.0f };
	//const bx::Vec3 eye = { 0.0f, 0.0f, -35.0f };

	//// Set view and projection matrix for view 0.
	//{
	//	g_time += 0.01f;

	//	float view[16];
	//	bx::mtxLookAt(view, eye, at);

	//	float proj[16];
	//	bx::mtxProj(proj, 60.0f, float(1024) / float(1024), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
	//	bgfx::setViewTransform(0, view, proj);

	//	float mtx[16];
	//	bx::mtxIdentity(mtx);
	//	bx::mtxRotateXY(mtx, g_time + 1 * 0.21f, g_time + 1 * 0.37f);
	//	//mtx[12] = -15.0f + float(xx) * 3.0f;
	//	//mtx[13] = -15.0f + float(yy) * 3.0f;
	//	//mtx[14] = 0.0f;

	//	// Set model matrix for rendering.
	//	bgfx::setTransform(mtx);

	//	// Set vertex and index buffer.
	//	bgfx::setVertexBuffer(0, g_vb);

	//	//bgfx ����춛]�������᣿
	//	bgfx::setIndexBuffer(g_ib);

	//	uint64_t state = 0
	//		| BGFX_STATE_WRITE_MASK  //1.write mask ��rgb a z��
	//		| BGFX_STATE_DEPTH_TEST_LESS //2.z test(less lessequal...
	//		| BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_ONE, BGFX_STATE_BLEND_ZERO) //3.alphablend 
	//		//or BGFX_STATE_BLEND_FUNC_SEPARATE;
	//		//4.BLEND_EQUATION �󲿷�����¶���ʹ��Func_add���������ò�����
	//		| BGFX_STATE_CULL_CW //5 cull �� //CW CCW or none
	//		//| BGFX_STATE_PT_TRISTRIP //6 primitive type��Ҳ��state�У����Ӿ���trilist ����ģʽҲ�Ƿ���state����ģ�
	//		//| BGFX_STATE_MSAA 7.msaa 

	//		;
	//	// Set render states.
	//	bgfx::setState(state);

	//	// Submit primitive for rendering to view 0.
	//	bgfx::submit(0, g_program);
	//}
	////debug���ܲ�֧��rt Ҳ��֧�� swapchain��ֻ�ܮ�������Ļ��
	////bgfx::setDebug(BGFX_DEBUG_STATS);
	//bgfx::frame();
}
void app_inneltest::onEditorGUI()
{
	// if (ImGui::Begin("__testrt__", 0, ImGuiWindowFlags_NoCollapse))
	// {
	//	ImGui::Text("helloha");
	//	//texID �������԰����Ĵ����ֽ���Ϣ����Ϣ���˾͵���ַ�ã�Ŀǰ����Ҫ�ܶ���Ϣ������ֱ�ӱ���
	//	uint32_t mem = 0;
	//	((uint16_t*)&mem)[0] = g_frame_tex.idx;
	//	((uint8_t*)&mem)[2] = 1;//1 =IMGUI_FLAGS_ALPHA_BLEND
	//	((uint8_t*)&mem)[3] = 0;
	//	auto TexID = (ImTextureID)(size_t)mem;


	//	ImGui::Image(TexID, ImVec2(ImGui::GetWindowWidth(), 256));
	//	for (int i = 0; i < 10; i++)
	//		ImGui::Button("haha?");
	//	ImGui::End();
	//}
	//if (ImGui::Begin("test img2"))
	//{
	//	uint32_t mem = 0;
	//	((uint16_t*)&mem)[0] = g_ctex.idx;
	//	((uint8_t*)&mem)[2] = 1;//1 =IMGUI_FLAGS_ALPHA_BLEND
	//	((uint8_t*)&mem)[3] = 0;
	//	auto TexID = (ImTextureID)(size_t)mem;
	//	ImGui::Image(TexID, ImVec2(256, 256));
	// }
	// ImGui::End();
}

void app_inneltest::onExit()
{

}